package com.finddeer.bookkeeping.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.R;

import java.util.Calendar;
import java.util.List;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/23 8:23 PM
 * function  :
 */
public class BudgetFragment extends Fragment {

    private EditText mIncomeSetEditText;
    private EditText mPaySetEditText;
    private TextView mRemainTextView;
    private TextView mSpendTextView;
    private Button mIncomeButton;
    private Button mPayButton;
    private double income = 0.0;
    private double pay = 0.0;
    private float[] b = {0};//暂存每条bill的amount
    public BudgetFragment() {

    }

    public int getDayOfMonth(int year,int month) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_budget, container, false);

        mIncomeSetEditText = view.findViewById(R.id.editText_income);
        mIncomeSetEditText.setText(String.valueOf(income));

        mIncomeButton = view.findViewById(R.id.button_income_ensure);
        mIncomeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                income = Double.parseDouble(mIncomeSetEditText.getText().toString());
                mIncomeSetEditText.setText(String.valueOf(income));
            }
        });

        mPaySetEditText = view.findViewById(R.id.editText_pay);
        mPaySetEditText.setText(String.valueOf(pay));

        mPayButton = view.findViewById(R.id.button_pay_ensure);
        mPayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pay = Double.parseDouble(mPaySetEditText.getText().toString());
                mPaySetEditText.setText(String.valueOf(pay));
                BillLab billLab = BillLab.get(getActivity());
                List<Bill> bills = billLab.getBills();

                for (int i = 0; i < bills.size(); i++) {
                    pay -= bills.get(i).getAmount();
                }

                mRemainTextView = view.findViewById(R.id.textView_remain);
                mRemainTextView.setText(String.valueOf(pay));

                Calendar calendar = Calendar.getInstance();
                int month = calendar.get(Calendar.MONTH)+1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);

                mSpendTextView = view.findViewById(R.id.textView_spend_max);
                mSpendTextView.setText(String.valueOf((pay/getDayOfMonth(month,day))));
            }
        });

        return view;
    }
}
